STDOUT.sync = true
require 'Json.rb'

if (ARGV[0] == nil) then
	url = "202.249.24.19"
	path = "/cgi-bin/limura/Game/status.cgi"
	port = 80
	filename = "tmp_json_status.txt"
else
	puts "Option"
	filename = ARGV[0]
	url = ARGV[1]
	path = ARGV[2]
	port = ARGV[3]

	filename2 = ARGV[4]
	url2 = ARGV[5]
	path2 = ARGV[6]
	port2 = ARGV[7]
end

printf("Datafetch.rb Launch: %s %s %s %s %s %s %s %s\n", url, path, port, filename, url2, path2, port2, filename2)

while (true) do
	sleep(2)
	day = Time.now
	output = sprintf("%02d:%02d:%02d Datafetch.rb: Fetch begin: %s\n",
		 day.hour, day.min, day.sec, url, path, port)
	printf(output)

	data = Json::get_jsonobj(url, path, port)
	data2 = Json::get_jsonobj(url2, path2, port2)

	day = Time.now
	output = sprintf("%02d:%02d:%02d Datafetch.rb: Data1: %d bytes Data2: %d bytes\n",
		 day.hour, day.min, day.sec, data.size, data2.size)
	printf(output)


	day = Time.now
	output = sprintf("%02d:%02d:%02d Datafetch.rb: Fetch end:\n",
		 day.hour, day.min, day.sec)
	printf(output)

	f = File::open(filename, "w")
	f.puts(data)
	f.close
	f = File::open(filename2, "w")
	f.puts(data2)
	f.close


end
